package view;

import java.awt.Color;
import java.awt.Dimension;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JComboBox;
import javax.swing.JPanel;

import model.Conference;
import model.State;

/**
 * Class to create a conf List Panel.
 * @author Sarah George & Kevin Anderson
 * @version Autumn 2012
 */
@SuppressWarnings("serial")
public class ConfListPanel extends JPanel implements Observer {
	
	/**
	 * Field to hold the combo box of conferences.
	 */
	JComboBox<Conference> my_comboBox;
	
	/**
	 * Field to hold the Conf Listener.
	 */
	ConfListener my_confListener;
	
	/**
	 * Constructor for a ConfListPanel.
	 */
	public ConfListPanel() {
		super();
		setBackground(Color.LIGHT_GRAY);
		setPreferredSize(new Dimension(500, 50));
		my_comboBox = new JComboBox<Conference>();
		my_comboBox.setVisible(false);
		this.add(my_comboBox);
		my_confListener = new ConfListener();
	}

	/**
	 * Method  to fill combo box with conferences when observable says it is ready.
	 * @param o the observable
	 * @param arg the object that called notify observers.
	 */
	@Override
	public void update(Observable o, Object arg) {
		if (arg instanceof Conference[]) {
			my_comboBox.removeActionListener(my_confListener);  //must remove action listener during updates so false events don't trigger.
			my_comboBox.removeAllItems(); //refresh list before repopulating
			
			for (Conference conf : (Conference[]) arg)
				my_comboBox.addItem(conf);
			
			my_comboBox.setVisible(true);
			my_confListener.setState((State) o);
			my_comboBox.addActionListener(my_confListener);
			this.validate();
		}
	}
}//End Class ConfListPanel.